package com.qigao.mall.service.channel;

import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.param.CommonReqParam;
import com.qigao.mall.channel.ChannelCore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/5/28 11:19
 * @Descripton:渠道层统一处理
 * @Modify :
 **/
@Service
public  class ChannelServiceImpl implements ChannelService {
    private static final Logger logger= LoggerFactory.getLogger(ChannelServiceImpl.class);
    @Autowired
    List<ChannelCore> channelCoreList;

    private ChannelCore getByPayWay(String productCode, String channelAction,String channelCode) {
        return channelCoreList.stream().filter(s->s.isSupport(PayProductEnum.parse(productCode), ChannelActionEnum.parse(channelAction), ChannelInfoEnum.parse(channelCode))).findFirst().get();
    }

    @Override
    public ChannelRespParam channelProcess(MerChannelDto merChannelDto, CommonReqParam commonReqParam) {
        ChannelCore channelCore =getByPayWay(merChannelDto.getProductNo(), merChannelDto.getChannelAction(),merChannelDto.getChannelCode());
        ChannelReqParam channelReqParam=new ChannelReqParam();
        BeanUtils.copyProperties(commonReqParam,channelReqParam);
        logger.info("[渠道层]配置参数{},请求第三方参数:{}",merChannelDto,channelReqParam);
        ChannelRespParam channelRespParam= channelCore.channelProcess(merChannelDto,channelReqParam);
        logger.info("[渠道层]请求第三方响应:{}",channelRespParam);
        return channelRespParam;
    }
}
